package com.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.R;

import com.example.pojo.Dev;
import com.example.service.DevService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 能用到的自己使用的接口--收发接口
 */
@Controller
public class gatawayController {
//    @Resource
//    private  InServerHandler   inServerHandler;
    @Resource
  private DevService devService;
    @GetMapping("/show/{address}")//接收网关发来的数据
    public String  RXD( @PathVariable("address") String address, Model model) {

        model.addAttribute("address",address);
        QueryWrapper<Dev> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address",address);
        Dev dev1 =devService.getOne(queryWrapper);
        if(dev1==null) { model.addAttribute("msg","{暂无数据发送}");}
        else  {
              //将字符串转换成json
            JSONObject jsonObject = JSONObject.parseObject(dev1.getMsg());
              //取出data里的数据
            model.addAttribute("type",jsonObject.getJSONObject("msg").getString("type"));
            model.addAttribute("version",jsonObject.getJSONObject("msg").getString("version"));
            model.addAttribute("iccid",jsonObject.getJSONObject("msg").getString("iccid"));
            model.addAttribute("imei",jsonObject.getJSONObject("msg").getString("imei"));
            model.addAttribute("mac",jsonObject.getJSONObject("msg").getString("mac"));
            model.addAttribute("runtime",jsonObject.getJSONObject("msg").getString("runtime"));
            model.addAttribute("DDR",jsonObject.getJSONObject("msg").getString("DDR"));
            model.addAttribute("signal",jsonObject.getJSONObject("msg").getString("signal"));
            model.addAttribute("gps",jsonObject.getJSONObject("msg").getString("gps"));
            model.addAttribute("msg",jsonObject);
            }
      return "show";
    }
    @GetMapping("/RXD1")//给网关发送的数据
    @ResponseBody
    public R RXD1() {
        return R.ok();
    }

    @ResponseBody
    @GetMapping("/deploy/{address}")//跳到对应的配置页面
    public String deploy(@PathVariable("address") String address,Model model) {
         model.addAttribute("address",address);
         return "deploy";
    }
    @ResponseBody
    @GetMapping("/modbus/{address}")//跳到对应的/modbus页面
    public String modbus(@PathVariable("address") String address,Model model) {
        model.addAttribute("address",address);
        return "modbus";
    }
}
